package me.hetong.caigou.entity;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.log.StaticLog;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import me.hetong.caigou.ui.view.Right;

import static me.hetong.caigou.Global.config;

/**
 * 提示消息
 */
public class MessageBean {
    private VBox vBox = new VBox();
    private Label label = new Label();
    private AnchorPane root;

    public MessageBean(AnchorPane root) {
        this.root = root;
        this.label.setTextFill(Color.WHITE);
        this.vBox.setPadding(new Insets(10, 20, 10, 20));
        this.vBox.getChildren().add(label);
        this.vBox.setVisible(false);
        this.vBox.setManaged(false);
        AnchorPane.setBottomAnchor(vBox, 10.0);
        AnchorPane.setRightAnchor(vBox, 10.0);
        root.getChildren().add(vBox);
    }

    public void info(String... text) {
        ThreadUtil.execute(() -> Platform.runLater(() -> {
            vBox.setStyle("-fx-background-color: #F3B044ef;-fx-border-radius: 5;-fx-background-radius: 5;");
            createMessage(text);
        }));
    }

    private void createMessage(String[] text) {

        this.root.getChildren().remove(vBox);
        this.root.getChildren().add(vBox);

        StringBuilder sb = new StringBuilder();
        for (String s : text) {
            sb.append(s);
            sb.append(" ");
        }

        StaticLog.info("{}", sb);

        label.setText(sb.toString());
        vBox.setVisible(true);
        vBox.setManaged(true);

        ThreadUtil.execute(() -> {
            ThreadUtil.sleep(config.getMessageWaitingTime());
            vBox.setVisible(false);
            vBox.setManaged(false);
        });
    }
}